package com.example.myworldcups;

import java.sql.*;

/**
 * @author lenovo
 * @date 2022/12/30
 */
public class Demo {
    public String sayHello(String name) {
        return "Hello " + name + "!";
    }

    public void toMysql(String sql) throws ClassNotFoundException, SQLException {
        //1.注册数据库的驱动
        Class.forName("com.mysql.jdbc.Driver");
//2.获取数据库连接（里面内容依次是："jdbc:mysql://主机名:端口号/数据库名","用户名","登录密码"）
        Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/demo", "root", "root");
//3.需要执行的sql语句（?是占位符，代表一个参数）
//        String sql = "insert into stu(id,name,age) values(?,?,?)";
//4.获取预处理对象，并依次给参数赋值
        PreparedStatement pst = connection.prepareStatement(sql);
//        statement.setInt(1, 12); //数据库字段类型是int，就是setInt；1代表第一个参数
//        statement.setString(2, "小明");    //数据库字段类型是String，就是setString；2代表第二个参数
//        statement.setInt(3, 16); //数据库字段类型是int，就是setInt；3代表第三个参数
//5.执行sql语句（执行了几条记录，就返回几）
        ResultSet rs = pst.executeQuery();
        while (rs.next()) {
            System.out.println(rs.getInt("world_cups_summary_id") +
                    "::" + rs.getString("Year") +
                    "::" + rs.getString("Host_Country") +
                    "::" + rs.getString("Winner") +
                    "::" + rs.getString("Second") +
                    "::" + rs.getString("Third") +
                    "::" + rs.getString("Fourth") +
                    "::" + rs.getString("Goals_Scored") +
                    "::" + rs.getString("Qualified_Teams") +
                    "::" + rs.getString("Matches_Played") +
                    "::" + rs.getString("Attendance") +
                    "::" + rs.getString("Host_Continent") +
                    "::" + rs.getString("Winner_Continent"));
        }
//6.关闭jdbc连接
        rs.close();
        pst.close();
        connection.close();
    }
}
